# 定义一个有序字典
from collections import OrderedDict
from pprint import pprint

from django.http import HttpResponseNotFound

from apps.goods.models import GoodsCateogory, Goods


def get_category():
    """获取商品分类"""

    category_dict = OrderedDict()
    # 获取所有分类数据

    categorys = GoodsCateogory.objects.filter(parent_category__isnull=True).all()  # 获取一级分类

    for category_1 in categorys:
        group_id = category_1.id  # 以当前一级分类的id作为整个字典的键
        # print("存的时候的类型:", type(group_id))
        category_dict[group_id] = {'channels': [], 'sub_cat': []}
        # print("存分类时", category_1.id)
        # print("存分类时", type(category_1.id))
        category_dict[group_id]["channels"] = [{
            "id": category_1.id,  # 一级分类的id
            'name': category_1.name,  # 一级分类的名字
            "image": category_1.image,  # 一级分类显示的图片
        }]
        for category_2 in category_1.sub_cat.all():  # 获取到所有二级分类的名字
            group_id_02 = category_2.id
            # print("2级分类id", category_2.id, type(category_2.id))
            category_dict[group_id]["sub_cat"].append({  # 添加所有2级分类的数据到1级分类的sub_cat中，因为2级分类不会只有1个
                'id': int(category_2.id),
                'name': category_2.name,
                'image': category_2.image,  # 但是大部分2级分类没有图片，所以不用填充图片
            })
    return category_dict


def get_category_goods(category_id):
    """
    获取商品数据
    :param category_id:
    :return:
    """
    goods_dict = {}
    try:
        cate = GoodsCateogory.objects.get(id=category_id)
        # print(cate)
    except GoodsCateogory.DoesNotExist:
        return HttpResponseNotFound('GoodsCategory does not exist')
    if cate.parent_category:  # 2级
        a = cate.category_set.all()
        for item in a:
            # print(item.goods.name)
            goods_dict[item.goods.id] = {
                "id": item.goods.id,
                "name": item.goods.name,
                "image": item.goods.front_image,
                "current_price": item.goods.current_price,
            }

    else:
        for category_2 in cate.sub_cat.all():  # 循环出2级分类
            data = category_2.category_set.all()
            for item in data:
                # print(item.goods.name)
                goods_dict[item.goods.id] = {
                    "id": item.goods.id,
                    "name": item.goods.name,
                    "image": item.goods.front_image,
                    "current_price": item.goods.current_price,
                }
    return goods_dict


def get_goods(category_id):
    """
    获取商品数据
    :param category_id:
    :return:
    """
    goods = []
    try:
        cate = GoodsCateogory.objects.get(id=category_id)
        # print(cate)
    except GoodsCateogory.DoesNotExist:
        return HttpResponseNotFound('GoodsCategory does not exist')
    if cate.parent_category:  # 2级
        a = cate.category_set.all()
        for item in a:
            # print(item, 'item')
            goods.append(item.goods)


    else:
        for category_2 in cate.sub_cat.all():  # 循环出2级分类
            a = category_2.category_set.all()
            for item in a:
                # print(item, 'item')
                goods.append(item.goods)

    print(32222)
    print(goods)
    return goods


def data_get_goods(good_id):
    """
    获取商品数据
    :param category_id:
    :return:
    """
    goods_dict = {}
    try:
        good = Goods.objects.get(id=good_id)  # 通过传入的商品id查询对应的商品
    except Goods.DoesNotExist:
        return None

    goods_dict["id"] = good.id
    goods_dict["name"] = good.name
    goods_dict['front_image'] = good.front_image
    goods_dict["current_price"] = good.current_price
    goods_dict['sales'] = good.sales
    goods_dict['slave'] = good.slave  # 剩余库存
    images = []  # 用于存储商品详情图片的列表
    for image in good.img_goods.all():
        images.append(image.image)
    goods_dict["good_images"] = images  # 商品详情图片
    # pprint(goods_dict)
    return goods_dict


if __name__ == '__main__':
    data_get_goods(58)  # 测试
